package com.jiangxiaofei.citycloud.activity.freshfruits;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.adapter.FreshCassifyAdapter;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.FreshFruitCassifyBean;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FreshConstant;
import com.jiangxiaofei.citycloud.mvp.FreshPersenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * @author: JiangXFei
 * @date: 2017/11/21 0021
 * @content:
 */

public class FreshFruitsCassifyActivity
        extends BaseActivity
        implements FreshConstant.GetFreshCassifyView {


    @InjectView(R.id.recy_fresh_cassify)
    RecyclerView recyFreshCassify;
    @InjectView(R.id.recy_fresh_cassify_main)
    RecyclerView recyFreshCassifyMain;

    private List<FreshFruitCassifyBean> cassifyBeanMenuList = new ArrayList<>();
    private List<FreshFruitCassifyBean> cassifyBeanItemList = new ArrayList<>();
    private FreshConstant.GetFreshCassifyPersenter getFreshCassifyPersenter;
    private int id = 0;
    private FreshCassifyAdapter cassifyAdapter;
    private FreshCassifyAdapter.FreshCassifyItemAdapter itemAdapter;
    private TextView tvCassifyTitle;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fresh_fruits_cassify_layout);
        ButterKnife.inject(this);
        mContext = this;
        mActivity = this;
        initTitle();
        initMenuAdapter();
        initItemAdapter();
        getFreshCassifyPersenter = new FreshPersenter(mContext, (FreshConstant.GetFreshCassifyView) mActivity);
        getFreshCassifyPersenter.getFreshCassify();
    }

    /**
     * 初始化小条目适配器
     */
    private void initItemAdapter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 4);
        itemAdapter = new FreshCassifyAdapter.FreshCassifyItemAdapter(R.layout.item_fresh_cassify_layout, cassifyBeanItemList);
        recyFreshCassify.setLayoutManager(gridLayoutManager);
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.header_fresh_cassify_type_title, null);
        tvCassifyTitle = headerView.findViewById(R.id.tv_cassify_title);
        itemAdapter.addHeaderView(headerView);
        itemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, FreshFruitGoodListActivity.class);
                intent.putExtra(StringConstants.FRESHGOODCASSIFYID, id);
                intent.putExtra(StringConstants.FRESHGOODTYPEID, Integer.valueOf(cassifyBeanItemList.get(position).getId()));
                startActivity(intent);
            }
        });
        recyFreshCassify.setAdapter(itemAdapter);
    }

    /**
     * 初始化大分类适配器
     */
    private void initMenuAdapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        cassifyAdapter = new FreshCassifyAdapter(R.layout.item_fresh_cassify_type_layout, cassifyBeanMenuList);
        recyFreshCassifyMain.setLayoutManager(layoutManager);
        cassifyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < cassifyBeanMenuList.size(); i++) {
                    cassifyBeanMenuList.get(i).setChecked(false);
                }
                cassifyBeanMenuList.get(position).setChecked(true);
                cassifyAdapter.notifyDataSetChanged();
                id = Integer.valueOf(cassifyBeanMenuList.get(position).getId());
                getFreshCassifyPersenter.getFreshCassify();
            }
        });
        recyFreshCassifyMain.setAdapter(cassifyAdapter);
    }

    @Override
    protected void setListener() {
        setLeftOneBg(R.drawable.ic_back);
        setCTitle("商品分类");
        rl_layout.setBackgroundColor(Color.parseColor("#66c9ff"));
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public int getFreshCassifyId() {
        return id;
    }

    @Override
    public void getFreshCassifyOnSuccess(List<FreshFruitCassifyBean> cassifyBeans) {
        if (cassifyBeans.size() > 0) {
            if (cassifyBeanMenuList.size() == 0) {
                cassifyBeanMenuList.addAll(cassifyBeans);
                cassifyBeanMenuList.get(0).setChecked(true);
                cassifyAdapter.notifyDataSetChanged();
                id = 1;
                getFreshCassifyPersenter.getFreshCassify();
            } else {
                tvCassifyTitle.setText(cassifyBeanMenuList.get(id - 1).getName());
                cassifyBeanItemList.clear();
                cassifyBeanItemList.addAll(cassifyBeans);
                itemAdapter.notifyDataSetChanged();
            }
        }
    }
}
